﻿using System;
using System.Net;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using JustinAngel.Neurons.AppCore;
using JustinAngel.Neurons.Framework;
using JustinAngel.Neurons.Framework.Navigation;
using JustinAngel.Neurons.Framework.Helpers;

namespace JustinAngel.Neurons.Pages.Main
{
    [DataContract]
    public class VideoItemViewModel
    {
        public VideoItemViewModel()
        {
        }

        public VideoItemViewModel(Uri youtubeUrl, string title)
        {
            YoutubeUrl = youtubeUrl;
            Title = title;
            IsRestoreFromMediaTile = true;
        }

        public VideoItemViewModel(Uri imageUrl, Uri youtubeUrl, Uri externalLink, string title, string groupDisplayName, Uri highResImageUrl)
        {
            ImageUrl = imageUrl;
            YoutubeUrl = youtubeUrl;
            ExternalLink = externalLink;
            Title = title;
            GroupDisplayName = groupDisplayName;
            HighResImageUrl = highResImageUrl;
        }

        [DataMember]
        public Uri ImageUrl { get; set; }

        [DataMember]
        public Uri YoutubeUrl { get; set; }

        [DataMember]
        public Uri ExternalLink { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public string GroupDisplayName { get; set; }

        public ICommand NavigateToVideo
        {
            get
            {
                return new RelayCommand(() => DispatcherTimerHelper.On(TimeSpan.FromMilliseconds(200), () =>
                    //IoC.Get<IMessenger>().Send(new NavigateToMessage(PageSources.VideoPlayerPage, this))));
                    TaskInvoker.OpenWebBrowser("http://m.youtube.com/watch?v=" + this.YouTubeID)));
            }
        }

        public string YouTubeID
        {
            get
            {
                return YoutubeUrl.OriginalString
                    .Remove("http://gdata.youtube.com/feeds/api/videos/")
                    .Remove("http://www.youtube.com/watch?v=");
            }
        }

        /// <summary>
        /// perf optimization. There are 100+ of this class optimized so we don't want to call
        /// IoC.Build(this);
        /// </summary>
        public ITaskInvoker TaskInvoker { get { return IoC.Get<ITaskInvoker>(); } }

        public ICommand SendInEmail
        {
            get
            {
                return new RelayCommand(() =>
                    TaskInvoker.SendEmail(Title,
                    "Hi, " + Environment.NewLine + Environment.NewLine +
                    "I wanted to share this video from the Windows Phone 7 \"Neurons\" app:"
                    + Environment.NewLine + "\t" +
                    this.Title + Environment.NewLine + "\t"
                    + this.ExternalLink
                    )
                    );
            }
        }

        public ICommand ViewInBrowser
        {
            get
            {
                return new RelayCommand(() =>
                    TaskInvoker.OpenWebBrowser(this.ExternalLink.OriginalString)
                );
            }
        }

        public ICommand TweetInBrowser
        {
            get
            {
                return new RelayCommand(() =>
                    IoC.Get<IMessenger>().Send(new NavigateToMessage(PageSources.WebBrowser, TwitterUri)));
            }
        }

        public Uri TwitterUri
        {
            get
            {
                return new Uri(string.Format(@"http://m.twitter.com/share?url={0}&text={1}",
                                     HttpUtility.UrlEncode(this.ExternalLink.OriginalString),
                                     HttpUtility.UrlEncode(this.Title + " via @JustinAngel's Neurons app @ ")), UriKind.Absolute);
            }
        }

        public ICommand FacebookInBrowser
        {
            get
            {
                return new RelayCommand(() =>
                    IoC.Get<IMessenger>().Send(new NavigateToMessage(PageSources.WebBrowser, FacebookUri)));
            }
        }

        public Uri FacebookUri
        {
            get
            {
                return new Uri(string.Format(@"http://m.facebook.com/sharer.php?u={0}&t={1}",
                                     HttpUtility.UrlEncode(this.ExternalLink.OriginalString),
                                     HttpUtility.UrlEncode(this.Title)), UriKind.Absolute);
            }
        }

        [DataMember]
        public Uri HighResImageUrl { get; set; }

        public bool IsRestoreFromMediaTile { get; set; }
    }
}
